﻿#include <stdafx.h>
#include "DimensionTableView.h"
#include "QTableView"
#include "qboxlayout.h"
#include "QStandardItemModel"
#include "qheaderview.h"
#include <QDebug>
#include <QTimer>
//#include "ui_NetworkParamWidget.h"

class DimensionTableView::Internal
{

private:
	QTimer timer;
	int time = 0;

public:
	Internal(DimensionTableView* ptr)
	{
		q = ptr;
	}
	void initControl()
	{
		m_pTableView = new QTableView(q);
		m_Model = new QStandardItemModel(q);

		//设置列表头
		QStringList cols;
		cols << tr("机型")
			<< tr("总计数")
			<< tr("总不良数")
			<< tr("总不良率")
			<< tr("项目")
			<< tr("不良计数")
			<< tr("不良率");

		m_Model->setColumnCount(cols.count());
		for (int col = 0; col < cols.count(); col++) {
			m_Model->setHeaderData(col, Qt::Horizontal, cols[col]);
		}
		m_Model->setRowCount(35);
		// 设置字体大小为15
		QFont font = m_pTableView->font();
		font.setPointSize(10);
		m_pTableView->setFont(font);
		m_pTableView->setModel(m_Model);

		QVector<int> spinNumber = { 0,3,4,3,4,2,2,10,5,1,1,2 };
		QStringList machineList = { "正极表面","正极极耳","负极表面","负极极耳","上隔膜CCD",
			"下隔膜CCD","CCD蛇形NG","外观检查","正极行驶","负极行驶","端面" };

		QStringList projectList = {
		"项目尺寸","Defect","连接胶带",
		"切割尺寸","Scrap","外观","Burr",
		"极片尺寸","Defect","连接胶带",
		"切割尺寸","Scrap","外观","Burr",
		"尺寸","defect",
		"尺寸","defect",
		"CCD蛇形隔膜","CCD正极蛇形","CCD负极蛇形","蛇形F1","蛇形F2","正极极耳翻折","负极极耳翻折","正负极逆转","AW1","CW1",
		"外径","J/R隔膜宽度","终极胶尺寸","极耳成型角度","J/R表面",
		"极耳长度",
		"极耳长度",
		"直径","瑕疵"
		};

		for (int i = 0; i < projectList.length(); i++)
		{
			m_Model->setItem(i, 4, new QStandardItem(projectList[i]));
		}

		int total = 0;

		for (int var = 0; var < spinNumber.length(); var++)
		{
			for (int i = 0; i < 4; i++)
			{
				m_pTableView->setSpan(spinNumber[var]+ total, i, spinNumber[var+1], 1);
			}
			if (machineList.length()>var)
				m_Model->setItem(spinNumber[var] + total, 0, new QStandardItem(machineList[var]));

			total += spinNumber[var];
		}

		 //行号隐藏
		m_pTableView->verticalHeader()->setVisible(false);
		//行表头文字居中，默认列居中行左对齐
		m_pTableView->verticalHeader()->setDefaultAlignment(Qt::AlignCenter);
		m_pTableView->verticalHeader()->setDefaultSectionSize(23);//<! 默认行宽
		//设置列宽，设置后很多情况会导致重置model data后恢复默认列宽
		m_pTableView->setColumnWidth(0, 100);
		m_pTableView->setColumnWidth(1, 70);
		m_pTableView->setColumnWidth(2, 70);
		m_pTableView->setColumnWidth(3, 70);
		m_pTableView->setColumnWidth(4, 70);
		m_pTableView->setColumnWidth(5, 70);
		m_pTableView->setColumnWidth(6, 70);
		m_pTableView->setColumnWidth(7, 70);

		//最后一列填充宽度(宽度不够时)
		m_pTableView->horizontalHeader()->setStretchLastSection(true);

		//滚动默认是按照item宽，设置为平滑滚动
		//平滑 QAbstractItemView::ScrollPerPixel
		//元素 QAbstractItemView::ScrollPerItem
		m_pTableView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
		m_pTableView->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
		m_pTableView->setEditTriggers(QAbstractItemView::NoEditTriggers);

		

		//设置定时器刷新时间
		int interval = 1000;
		timer.setInterval(interval);
		
		//连接定时器的timeout信号得到槽函数
		QObject::connect(&timer, &QTimer::timeout, [&]() {
			time++;
			m_pTableView->show();
		});

		// 启动定时器
		timer.start();

	}
	void initForm()
	{
		q->setTitle(tr("DIMENSION TABLE"));
		QVBoxLayout* mainLayout = new QVBoxLayout(q->getMainWnd());
		mainLayout->addWidget(m_pTableView);
	}
public:
	//Ui::NetworkWidget ui;
	DimensionTableView* q; 
	QTableView* m_pTableView;
	QStandardItemModel* m_Model;
};
#include "QHBoxLayout"
#include <random>
#include <QTimer>
DimensionTableView::DimensionTableView(QWidget* parent) :
	CAbstractCard(parent),
	p(new Internal(this))
{
	//p->ui.setupUi(this);
	p->initControl();
    p->initForm();
	//设置相应逻辑为队列连接
	connect(&SignalCenter::GetInstance(), SIGNAL(refreshTableViewData(QString, int, int)), this,
		SLOT(refreshData(QString, int, int)), Qt::QueuedConnection);
	
}

DimensionTableView::~DimensionTableView()
{
	delete p;
	p = nullptr;
}

void DimensionTableView::refreshData(QString data, int row, int column)
{
	p->m_Model->setItem(row, column, new QStandardItem(data));
	///p->m_pTableView->show();
}

bool DimensionTableView::event(QEvent* event)
{
	return CAbstractCard::event(event);
}